﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace Aioms.Controllers
{
    [DynamicApiController]
    public class RolePermissionController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public RolePermissionController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostList([FromForm] long eid, [FromForm] long rid)
        {
            List<long> pmids = await db.Queryable<RolePermission>().Where(rp => rp.Eid == eid && rp.Rid == rid).Select(rp => rp.Pmid).ToListAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = pmids,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <param name="pmids"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostSave([FromForm] long eid, [FromForm] long rid, [FromForm] long[] pmids)
        {
            // 先清空之前所有角色权限
            await db.Deleteable<RolePermission>().Where(rp => rp.Eid == eid && rp.Rid == rid).ExecuteCommandAsync();

            List<RolePermission> rolePermissions = new List<RolePermission>();
            foreach (long pmid in pmids)
            {
                rolePermissions.Add(new RolePermission { Rid = rid, Pmid = pmid });
            }
            await db.Insertable<RolePermission>(rolePermissions).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <param name="pmid"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostDelete([FromForm] long eid, [FromForm] long rid, [FromForm] long pmid) {
            await db.Deleteable<RolePermission>().Where(rp => rp.Eid == eid && rp.Rid == rid && rp.Pmid == pmid).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
